import { login, sysProfile, sysUser } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken(state, value) {
    state.token = value
    setToken(value)
  },
  setUserInfo(state, value) {
    state.userInfo = value
  },
  // 退出登录处理
  logout(state) {
    state.token = ''
    setToken('')
    state.userInfo = ''
  }
}
const actions = {
  // 登录调用
  async toLogin({ commit }, value) {
    const res = await login(value)
    commit('setToken', res.data)
    console.log('菜鸡', res)
  },
  // 获取个人用户信息
  async getUserInfo({ commit }) {
    // 获取员部分信息，不包含头像信息
    const res = await sysProfile()
    // 通过userdi获取其他信息
    const res2 = await sysUser(res.data.userId)
    commit('setUserInfo', { ...res.data, ...res2.data })
    console.log('浪费人生：', res, res2)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
